#coding=utf-8
__author__ = 'tyrion'

from flask import  Flask
from flask import request
from flask import  render_template

from src.status.InstanceStatus import  InstanceStatus
from src.status.ServerStatus import  ServerStatus

import  json
# import  rrdtool

app = Flask(__name__)


# def GetOneSvrStatus(SvrId):
#     print("info: start get server=%s status" % (SvrId))
#     url = "%s" %(SvrId)
#
#     result = {"status" : "up"}
#
#     return result

# def GetOneInstancesStatus(SvrId, InstanceId):
#     print("info: start get instance=%s%s status" %(SvrId, InstanceId))
#
#     url = "%s%s" %(SvrId, InstanceId)
#     print "info: url = %s" % (url)
#
#     result = {InstanceId: "up"}
#     return result

@app.route("/perdata", methods=['GET', 'POST'])
def GetInstancePerfData():

    data = request.get_data()
    print"info: request body = %s" %(data)
    instances = json.loads(data)
    print("info: json request body =%s") % (instances)

    result = []
    for instance in instances:
        resultTmp = {}
        instanceName = instance.get('instance', "not found instance name")
        print "info: instance name = %s" % instanceName
        svrName = instance.get('svr', "error: not found server name")
        print "info: server name = %s" %(svrName)
        starTime = instance.get('start', "warning: not found start time")
        endTime = instance.get('end', "warning: not found end time")

        cmd = ""

        result.append(resultTmp)

    jsonResult = json.dumps(result)
    return jsonResult

@app.route('/instance', methods=['GET', 'POST'])
def GetInstanceStatus():
    # print request.values
    # print request.get_data()
    data = request.get_data()
    print"info: request body = %s" %(data)
    instances = json.loads(data)
    print("info: json request body =%s") % (instances)

    result = []
    for instance in instances:
        instanceConf = {}
        instanceName = instance.get('instance', "not found instance name")
        print "info: instance name = %s" % instanceName
        instanceConf["instancename"] = instanceName
        svrName = instance.get('svr', "error: not found server name")
        print "info: server name = %s" %(svrName)
        instanceConf["svrname"] = svrName

        intanceTmp =  InstanceStatus(instanceConf)
        resultTmp = intanceTmp.GetStatus()
        # resultTmp = GetOneInstancesStatus(SvrId = svrName, InstanceId = instanceName)
        result.append(resultTmp)

    jsonResult = json.dumps(result)
    return jsonResult

@app.route("/svr", methods=['GET', 'POST'])
def GetSvrStatus():

    data = request.get_data()
    print"info: request body = %s" %(data)
    servers = json.loads(data)
    print("info: json request body =%s") % (servers)

    result = []
    for server in servers:
        serverConf = {}
        svrName = server.get('svr', "error: not found server name")
        print "info: server name = %s" %(svrName)
        serverConf['svrName'] = svrName
        serverTmp = ServerStatus(serverConf)
        resultTmp = serverTmp.GetStatus()
        # resultTmp = GetOneSvrStatus(SvrId = svrName)
        result.append(resultTmp)

    jsonResult = json.dumps(result)
    return  jsonResult

@app.route("/getmsg", methods=['GET', 'POST'])
def getMsg():


    # return render_template('msg.html');
    return  "亲爱的, 七夕快乐";



if __name__ == "__main__":
    app.run(debug = True, host="0.0.0.0")
